package com.hundredhealth.rose.delegate;

import android.content.res.AssetManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.hundredhealth.rose.R;
import com.hundredhealth.rose.model.Customer;
import com.hundredhealth.rose.widget.EmptyLayout;
import com.kymjs.frame.view.AppDelegate;

import java.io.File;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by huazhong on 27/6/2017.
 */

public class CustomerInputDelegate extends AppDelegate {

    private Button scanIDButton;

    private EditText mCustomerNameView;
    private EditText mCustomerPhoneView;
    private EditText mCustomerBirthdayView;
    private EditText mCustomerSexView;
    private EditText mCustomerIDNumberView;
    private EditText mFinancialAdvisorNameView;
    private EditText mFinancialAdvisorPhoneView;
    private EditText mSalesAddressView;
    private GifImageView mPrcessingView;
    private View mInputCustomerView;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_customer_input;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        scanIDButton = get(R.id.ocrButton);
        mCustomerNameView = get(R.id.customerName);
        mCustomerPhoneView = get(R.id.customerMobilePhone);
        mCustomerBirthdayView = get(R.id.birthday);
        mCustomerSexView = get(R.id.sex);
        mCustomerIDNumberView = get(R.id.idNumber);
        mFinancialAdvisorNameView = get(R.id.financialAdvisorName);
        mFinancialAdvisorPhoneView = get(R.id.financialAdvisorPhone);
        mSalesAddressView = get(R.id.salesAddress);
        mPrcessingView = get(R.id.img_processing_layout);
        mInputCustomerView = get(R.id.inputCustomer);
        try {
            AssetManager assetManager = getActivity().getResources().getAssets();
            String[] loadingList = assetManager.list(EmptyLayout.LOADING_IMAGE_FOLDER_NAME);
            String imageName = loadingList[((int) (loadingList.length * Math.random()))];
            String absoluteImageName = EmptyLayout.LOADING_IMAGE_FOLDER_NAME + File.separator + imageName;
            GifDrawable loadingDrawable = new GifDrawable(assetManager, absoluteImageName);
            mPrcessingView.setImageDrawable(loadingDrawable);
        } catch (Exception e) {
        }
    }

    @Override
    public int getOptionsMenuId() {
        return R.menu.customer_input_menu;
    }

    public void onOCRCallBackUpdate(String name, String birthday, String gender, String idNumber) {
        mCustomerNameView.setText(name);
        mCustomerBirthdayView.setText(birthday);
        mCustomerSexView.setText(gender);
        mCustomerIDNumberView.setText(idNumber);
    }

    public boolean doCustomerValidate() {
        String customerName = mCustomerNameView.getText().toString().trim();
        String customerPhone = mCustomerPhoneView.getText().toString().trim();
        String customerBirthday = mCustomerBirthdayView.getText().toString().trim();
        String customerSex = mCustomerSexView.getText().toString().trim();
        String customerIdNumber = mCustomerIDNumberView.getText().toString().trim();
        String financialAdvisorName = mFinancialAdvisorNameView.getText().toString().trim();
        String financialAdvisorPhone = mFinancialAdvisorPhoneView.getText().toString().trim();
        String salesAddress = mSalesAddressView.getText().toString().trim();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password, if the user entered one.
        if (TextUtils.isEmpty(customerName)) {
            mCustomerNameView.setError(getActivity().getString(R.string.error_invalid_customerName));
            focusView = mCustomerNameView;
            cancel = true;
        } else if (TextUtils.isEmpty(customerBirthday)) {
            mCustomerBirthdayView.setError(getActivity().getString(R.string.error_invalid_customerBirthday));
            focusView = mCustomerBirthdayView;
            cancel = true;
        } else if (TextUtils.isEmpty(customerSex)) {
            mCustomerSexView.setError(getActivity().getString(R.string.error_invalid_customerSex));
            focusView = mCustomerSexView;
            cancel = true;
        } else if (TextUtils.isEmpty(customerIdNumber)) {
            mCustomerIDNumberView.setError(getActivity().getString(R.string.error_invalid_customerIdNumber));
            focusView = mCustomerIDNumberView;
            cancel = true;
        }
        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
        }
        return !cancel;
    }

    public Customer getCustomer() {
        String customerName = mCustomerNameView.getText().toString().trim();
        String customerPhone = mCustomerPhoneView.getText().toString().trim();
        String customerBirthday = mCustomerBirthdayView.getText().toString().trim();
        String customerSex = mCustomerSexView.getText().toString().trim();
        String customerIdNumber = mCustomerIDNumberView.getText().toString().trim();
        String financialAdvisorName = mFinancialAdvisorNameView.getText().toString().trim();
        String financialAdvisorPhone = mFinancialAdvisorPhoneView.getText().toString().trim();
        String salesAddress = mSalesAddressView.getText().toString().trim();
        Customer customer = new Customer(customerName, customerPhone, customerBirthday,
                customerSex, customerIdNumber, financialAdvisorName, financialAdvisorPhone, salesAddress);
        return customer;
    }

    public void showProgress(boolean show) {
        mPrcessingView.setVisibility(show ? View.VISIBLE : View.GONE);
    }
}
